package com.yws.demo.word2pdf.libreController;

import org.jodconverter.DocumentConverter;
import org.jodconverter.document.DefaultDocumentFormatRegistry;
import org.jodconverter.office.OfficeException;
import org.jodconverter.office.OfficeManager;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * @Author: fxy
 * @Date: 2023/02/17/15:01
 * @Description:
 */
@RestController
public class LibreTestController {

    @Resource
    private DocumentConverter documentConverter;

    @Resource
    private OfficeManager officeManager;

    @GetMapping("/libreTest")
    public void libreTest(InputStream file, HttpServletResponse response) throws IOException, OfficeException {

        /*documentConverter.convert(file).as(DefaultDocumentFormatRegistry.DOCX)
                .to(response.getOutputStream())
                .as(DefaultDocumentFormatRegistry.PDF)
                .execute();*/
        try {
            File file1 = new File("C:\\Users\\EDZ\\Desktop\\2022-301-310自来水.docx");
            File file2 = new File("C:\\Users\\EDZ\\Desktop\\2022-301-310自来水.pdf");
            documentConverter.convert(file1).to(file2).execute();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //officeManager.stop();
        }

    }

    @GetMapping("/libreTestStream")
    public void libreTestStream(MultipartFile file, HttpServletResponse response) throws IOException, OfficeException {
        try {
            documentConverter.convert(file.getInputStream()).as(DefaultDocumentFormatRegistry.DOCX)
                    .to(response.getOutputStream())
                    .as(DefaultDocumentFormatRegistry.PDF)
                    .execute();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //officeManager.stop();
        }

    }
}
